//
//  QQFriendHeader.swift
//  QQFriend
//
//  Created by zhang on 16/1/10.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit
//自定义协议
@objc protocol QQFriendHeaderShowFriendButtonClick
{
    optional func QQFriendHeaderShowFriendButtonClicked(tag:Int)
}
//自定义header类，注意继承自 UITableViewHeaderFooterView
class QQFriendHeader: UITableViewHeaderFooterView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    weak var buttonView:UIButton!
    weak var lableView:UILabel!
    weak var qqFriendGroup:QQFriendGroup!
        {
        didSet
        {
            self.assignSubViewContent()
        }
    }
    weak var delegate:QQFriendHeaderShowFriendButtonClick!//代理参数
    //可重用Header
    class func instance(tableView:UITableView)->QQFriendHeader
    {
        let identifier = "QQFriendSectionHeader"
        var header:UITableViewHeaderFooterView! = tableView.dequeueReusableHeaderFooterViewWithIdentifier(identifier)
        if header == nil
        {
            header = QQFriendHeader(reuseIdentifier: identifier)
        }
        let temp = header as! QQFriendHeader
        return temp
    }
    //重写父类的构造方法，添加需要的子控件
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        //自定义button，用于覆盖于view表层，响应点击时间
        let buttonView = UIButton(type: UIButtonType.Custom)
        buttonView.setImage(UIImage(imageLiteral: "buddy_header_arrow"), forState: UIControlState.Normal)
        buttonView.imageView?.contentMode = UIViewContentMode.Center//图片不压缩
        buttonView.imageView?.clipsToBounds = false//图片不裁剪
        buttonView.setBackgroundImage(UIImage(imageLiteral: "buddy_header_bg"), forState: UIControlState.Normal)//设置背景
        buttonView.setBackgroundImage(UIImage(imageLiteral: "buddy_header_bg_highlighted"), forState: UIControlState.Highlighted)
        buttonView.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left//设置内容堆对齐方式
        buttonView.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0)//设置内容边距
        buttonView.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)//设置标题颜色
        buttonView.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)//设置标题边距
        buttonView.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchDown)//绑定点击事件
        self.buttonView = buttonView
        let lableView = UILabel()
        self.lableView = lableView
        //加入父控件
        self.contentView.addSubview(self.buttonView)
        self.contentView.addSubview(self.lableView)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //给子控件赋值
    func assignSubViewContent()
    {
        let rotation = CGFloat(self.qqFriendGroup.hideGroup ? 0 : M_PI_2)//设置图片旋转，不设置的话，会出现因为重用而产生的bug
        self.buttonView.imageView?.transform = CGAffineTransformMakeRotation(rotation)
        self.buttonView.setTitle(self.qqFriendGroup.name as String, forState: UIControlState.Normal)
        self.lableView.text = "\(self.qqFriendGroup.online)/\(self.qqFriendGroup.friends.count)"
    }
    //往headerView中添加子控件的时候会调用
    //当改变了headerView的frame会调用
    override func layoutSubviews() {
        super.layoutSubviews()
        self.buttonView.setTitle(self.qqFriendGroup.name as String, forState: UIControlState.Normal)
        buttonView.frame = self.bounds
        
        //根据文字来设置labl的rect，用boundingRectWithSize的话需要字串是 NSString 格式，而且需要填一些参数，第二种简便些
        //lableText.boundingRectWithSize(CGSize.init(width: CGFloat.max, height: CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize()], context: nil)
        let lableSize = CGSizeMake((self.lableView?.textRectForBounds(CGRectMake(0, 0, CGFloat.max, CGFloat.max), limitedToNumberOfLines: 1).size.width)!, self.frame.size.height)
        
        lableView.frame = CGRectMake(self.frame.size.width - (lableSize.width + 10), 0, lableSize.width,lableSize.height)
    }
    //响应点击
    func buttonClick(sender:UIButton)
    {
        
        //缩放图片
        let rotation = CGFloat(self.qqFriendGroup.hideGroup ? M_PI_2 : 0)
        self.qqFriendGroup.hideGroup = self.qqFriendGroup.hideGroup ? false : true//修改显示状态
        UIView.animateWithDuration(0.5, animations: {
            sender.imageView?.transform = CGAffineTransformMakeRotation(rotation)
        })
        //注意这里调用的方式
        self.delegate?.QQFriendHeaderShowFriendButtonClicked?(self.tag)
    }
}
